/**
 * Class:   OrganizationEvaluationManager
 * 
 * Methods:
 * 
 *      boolean insertOrganizationEvaluation( int idActivity,
 *      									  OrganizationEvaluation organizationEvaluation );
 *      
 *      OrganizationEvaluation searchOrganizationEvaluation( int activityID );
 *      
 *      boolean deleteOrganizationEvaluation( int activityID );
 * 
 * Remarks:
 * 
 * 		This class should not be touched by the controllers.
 * 
 * @author ( Sharmaine Lim )
 * @version ( 2013-02-28 // YYYY-MM-DD )
 */

package managers;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import models.OrganizationEvaluation;

public class OrganizationEvaluationManager extends DatabaseManager {
    
    /**
     * Constructors
     */
    
    public OrganizationEvaluationManager() {}
    
    
    
    /**
     *  Insert Organization Evaluation
     *  	- inserts an organization evaluation into the
     *  	  evaluationOrganization database
     *  
     *  Parameters
     *  	- int activityID: the activity where the
     *  					  organization evaluation belongs
     *		- OrganizationEvaluation organizationEvaluation:
     *				the organizationEvaluation to be added
     *	
     *	Returns
     *		- boolean true: organizationEvaluation is
     *						successfully inserted
     * 		- boolean false: insertion failed
     */
    boolean insertOrganizationEvaluation( int activityID,
    									  OrganizationEvaluation organizationEvaluation ) {
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		
    		connect();
    		
    		preparedStatement = getConnection()
    				.prepareStatement( "INSERT INTO `evaluationOrganization`" +
    								   " (`activityID`, `dateFiled`" +
    								   " `multipleChoice1`," +
    								   " `multipleChoice2`," +
    								   " `multipleChoice3`," +
    								   " `multipleChoice4`," +
    								   " `multipleChoice5`," +
    								   " `multipleChoice6`," +
    								   " `multipleChoice7`," +
    								   " `multipleChoice8`," +
    								   " `multipleChoice9`," +
    								   " `multipleChoice10`," +
    								   " `multipleChoice11`," +
    								   " `multipleChoice12`," +
    								   " `multipleChoice13`," +
    								   " `multipleChoice14`," +
    								   " `multipleChoice15`," +
    								   " `multipleChoice16`," +
    								   " `multipleChoice17`," +
    								   " `multipleChoice18`," +
    								   " `multipleChoice19`," +
    								   " `multipleChoice20`," +
    								   " `multipleChoice21`," +
    								   " `multipleChoice22`," +
    								   " `multipleChoice23`," +
    								   " `multipleChoice24`," +
    								   " `multipleChoice25`," +
    								   " `multipleChoice26`," +
    								   " `multipleChoice27`, `question1`," +
    								   " `question2`, `question3`," +
    								   " `question4`, `question5`) VALUES" +
    								   " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?," +
    								   " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?," +
    								   " ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );

    		preparedStatement.setInt( 1, activityID );
    		preparedStatement.setString( 2, organizationEvaluation
    										.getDateFiled().toString() );
    		for ( int i = 0; i < 27; i++ ) {
        		
    			preparedStatement.setInt( (i + 3), organizationEvaluation
        										   .getMultipleChoiceAnswers()[i] );
    		}
    		for ( int i = 0; i < 5; i++ ) {
    			
				preparedStatement.setString( (i + 30), organizationEvaluation
								   				   .getStrAnswers()[i] );
			}
    		preparedStatement.executeUpdate();
    		
			disconnect();
    		
    		if ( searchOrganizationEvaluation( activityID ) != null ) {
    			return true;
    		}
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
	
	
	
	/**
	 *  Search Organization Evaluation
	 *  	- gets the organizationEvaluation in a certain activity
	 *  	  from the evaluationOrganization database
	 *  
	 *  Parameters
	 *		- int activityID: the ID of the activity the organizationEvaluation
	 *						  is from 
	 *	
	 *	Returns
	 *		- null: no organizationEvaluation is found
	 * 		- OrganizationEvaluation: organizationEvaluations is found
	 */
	OrganizationEvaluation searchOrganizationEvaluation( int activityID ) {
		
		/* Declaration of Local Variables */
		OrganizationEvaluation organizationEvaluation = null;
		
		try {
    		/* Declaration of Block Variables */
			Statement statement;
			ResultSet resultSet;
			
			connect();
			
			statement = getConnection().createStatement();
			
			resultSet = statement
						.executeQuery( "SELECT * FROM `evaluationOrganization`" +
									   " WHERE `activityID` = " + activityID );
			
			while ( resultSet.next() ) {
				int[] multipleChoice = new int[27];
				String[] strAnswers = new String[5];
				
				for ( int i = 0; i < 27; i++ ) {
					multipleChoice[i] = resultSet.getInt( "multipleChoice" +
														  (i + 1) );
					if ( i < 5 ) {
						strAnswers[i] = resultSet.getString( "question" +
															 (i + 1) );
					}
				}
				organizationEvaluation = new OrganizationEvaluation
						( parseDate( resultSet.getDate( "dateFiled" ) ),
								multipleChoice, strAnswers );
			}
		}
		catch ( Exception e ) {
			e.printStackTrace();
		}
		finally {
	    	if ( getConnection() != null ) {
	    		disconnect();
	    	}
		}
		return organizationEvaluation;
	}
    
    
    
    /**
     *  Update Organization Evaluation
     *  	- update an organization evaluation in the
     *  	  evaluationOrganization database
     *  
     *  Parameters
     *  	- int activityID: the activity where the
     *  					  organization evaluation belongs
     *		- OrganizationEvaluation overwrite: the new version
     *				of the organizationEvaluation to be updated
     *	
     *	Returns
     *		- boolean true: organizationEvaluation is
     *						successfully updated
     * 		- boolean false: updating failed
     */
    boolean updateOrganizationEvaluation( int activityID,
    									  OrganizationEvaluation overwrite ) {
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		
    		connect();
    		
    		preparedStatement = getConnection()
    				.prepareStatement( "UPDATE `evaluationOrganization` SET" +
    								   " `dateFiled` = ?," +
    								   " `multipleChoice1` = ?," +
    								   " `multipleChoice2` = ?," +
    								   " `multipleChoice3` = ?," +
    								   " `multipleChoice4` = ?," +
    								   " `multipleChoice5` = ?," +
    								   " `multipleChoice6` = ?," +
    								   " `multipleChoice7` = ?," +
    								   " `multipleChoice8` = ?," +
    								   " `multipleChoice9` = ?," +
    								   " `multipleChoice10` = ?," +
    								   " `multipleChoice11` = ?," +
    								   " `multipleChoice12` = ?," +
    								   " `multipleChoice13` = ?," +
    								   " `multipleChoice14` = ?," +
    								   " `multipleChoice15` = ?," +
    								   " `multipleChoice16` = ?," +
    								   " `multipleChoice17` = ?," +
    								   " `multipleChoice18` = ?," +
    								   " `multipleChoice19` = ?," +
    								   " `multipleChoice20` = ?," +
    								   " `multipleChoice21` = ?," +
    								   " `multipleChoice22` = ?," +
    								   " `multipleChoice23` = ?," +
    								   " `multipleChoice24` = ?," +
    								   " `multipleChoice25` = ?," +
    								   " `multipleChoice26` = ?," +
    								   " `multipleChoice27` = ?, `question1` = ?," +
    								   " `question2` = ?, `question3` = ?," +
    								   " `question4 = ?`, `question5` = ?" +
    								   " WHERE `activityID` = ?" );

    		preparedStatement.setString( 1, overwrite.getDateFiled().toString() );
    		for ( int i = 0; i < 27; i++ ) {
    			preparedStatement.setInt( (i + 2),
    									  overwrite.getMultipleChoiceAnswers()[i] );
    		}
    		for ( int i = 0; i < 5; i++ ) {
				preparedStatement.setString( (i + 29),
											 overwrite.getStrAnswers()[i] );
			}
    		preparedStatement.setInt( 34, activityID );
    		preparedStatement.executeUpdate();
    		
			disconnect();
    		
    		if ( searchOrganizationEvaluation( activityID ) != null ) {
    			return true;
    		}
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
    
    
    
    /**
     *  Delete Organization Evaluation
     *  	- deletes an organizationEvaluation from the
     *  	  evaluationOrganization database
     *  
     *  Parameters
     *  	- int activityID: the activity where the
     *  					  organizationEvaluation belongs
     *	
     *	Returns
     *		- boolean true: organizationEvaluation is successfully deleted
     * 		- boolean false: deletion failed
     */
    boolean deleteOrganizationEvaluation( int activityID ) {
    	try {
    		/* Declaration of Block Variables */
    		PreparedStatement preparedStatement;
    		
    		connect();
    		
    		if ( searchOrganizationEvaluation( activityID ) != null ) {
    			preparedStatement = getConnection()
        							.prepareStatement( "DELETE FROM" +
        											   " `evaluationOrganization`" +
        											   " WHERE `activityID`" +
        											   " = ?" );
	    		
	    		preparedStatement.setInt( 1, activityID );
	    		preparedStatement.executeUpdate();
	    		
				disconnect();
				
	    		if ( searchOrganizationEvaluation( activityID ) == null ) {
	    			return true;
	    		}
    		}
    	}
    	catch ( Exception e ) {
    		e.printStackTrace();
    	}
    	
    	if ( getConnection() != null ) {
    		disconnect();
    	}
    	return false;
    }
}
